/* WWTTTimerIBInspector.m
 * Written By:  Thomas Burkholder
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */
// hacked on by wave for WavesWorld

#import "WWTTTimerIBInspector.h"

@implementation WWTTTimer (AttributesInspector)

- (const char *)getInspectorClassName
{
	return "WWTTTimerIBInspector";
}

- (NXImage *)getIBImage
{
	return [NXImage findImageNamed:"WWTTTimer"];
}

@end

@implementation WWTTTimerIBInspector

static int thresholds[] = {NX_BASETHRESHOLD, 
							NX_RUNMODALTHRESHOLD, 
							NX_MODALRESPTHRESHOLD};
- init
{
	char buf[MAXPATHLEN+1];
	NXBundle *bundle;
	[super init];

   preControlStringSize = 256;
   preControlString = (char *)NXZoneCalloc([self zone], preControlStringSize, sizeof(char));

   conditionalSize = 256;
   conditional = (char *)NXZoneCalloc([self zone], conditionalSize, sizeof(char));

   controlStringSize = 256;
   controlString = (char *)NXZoneCalloc([self zone], controlStringSize, sizeof(char));

   postControlStringSize = 256;
   postControlString = (char *)NXZoneCalloc([self zone], postControlStringSize, sizeof(char));

	bundle = [NXBundle bundleForClass:[WWTTTimer class]];
	[bundle getPath:buf forResource:"WWTTTimerIBInspector" ofType:"nib"];
	[NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];

   // since the textView was "hidden" inside the ScrollView, we couldn't do this in IB, so...
   [preControlStringText setDelegate:self];
   [conditionalText setDelegate:self];
   [theControlStringText setDelegate:self];
   [postControlStringText setDelegate:self];

   [self revert:self];

	return self;
}

- free
{
  if (preControlString) { NXZoneFree([self zone], preControlString); }
  if (conditional) { NXZoneFree([self zone], conditional); }
  if (controlString) { NXZoneFree([self zone], controlString); }
  if (postControlString) { NXZoneFree([self zone], postControlString); }
  return [super free];
}

- ok:sender
{
  int   cnt;

  cnt = [preControlStringText textLength];
  while (cnt >= preControlStringSize)
  {  preControlStringSize *= 2;
     preControlString = (char *)NXZoneRealloc([self zone], preControlString, preControlStringSize);
  }
  *preControlString = '\0';
  [preControlStringText getSubstring:preControlString start:0 length:(cnt + 1)];
  [object setPreControlString:preControlString];

  cnt = [conditionalText textLength];
  while (cnt >= conditionalSize)
  {  conditionalSize *= 2;
     conditional = (char *)NXZoneRealloc([self zone], conditional, conditionalSize);
  }
  *conditional = '\0';
  [conditionalText getSubstring:conditional start:0 length:(cnt + 1)];
  [object setConditional:conditional];

  cnt = [theControlStringText textLength];
  while (cnt >= controlStringSize)
  {  controlStringSize *= 2;
     controlString = (char *)NXZoneRealloc([self zone], controlString, controlStringSize);
  }
  *controlString = '\0';
  [theControlStringText getSubstring:controlString start:0 length:(cnt + 1)];
  [object setControlString:controlString];

  cnt = [postControlStringText textLength];
  while (cnt >= postControlStringSize)
  {  postControlStringSize *= 2;
     postControlString = (char *)NXZoneRealloc([self zone], postControlString, postControlStringSize);
  }
  *postControlString = '\0';
  [postControlStringText getSubstring:postControlString start:0 length:(cnt + 1)];
  [object setPostControlString:postControlString];

	[window disableFlushWindow];
	[object setAutoStart:[autoStartSwitch state]];
	[object setPeriod:[periodField doubleValue]];
	[object setPriority:[priorityField intValue]];
	[object setVisibleDebug:[visibleDebugSwitch state]];
        if ([[behaviorMatrix selectedCell] tag] == 0) // it's a wrap
        {  [object setWrap:YES];
           [object setFiniteLife:NO];
           [object setConditionalLifespan:NO];
        }
        else
        {  if ([[behaviorMatrix selectedCell] tag] == 1) // it's days are numbered
           {  [object setWrap:NO];
              [object setFiniteLife:YES];
              [object setConditionalLifespan:NO];
           }
           else
           {  if ([[behaviorMatrix selectedCell] tag] == 2) // it's days are numbered
              {  [object setWrap:NO];
                 [object setFiniteLife:NO];
                 [object setConditionalLifespan:NO];
              }
              else  // it's conditional
              {  [object setWrap:NO];
                 [object setFiniteLife:YES];
                 [object setConditionalLifespan:YES];
              }
           }
        } 
	[object setSync:[syncSwitch state]];
	[object setSyncValue:[syncValueField intValue]];
        
	[object setStartValue:[startValueField floatValue]];
	[object setWrapValue:[behaviorValueField floatValue]];
	[object setIncrementBy:[incrementByField floatValue]];
	[self revertInspector:sender];
	[window reenableFlushWindow];
	[window flushWindow];
	return [super ok:sender];
}

- revertInspector:sender
{
   [autoStartSwitch setState:[object autoStart]];
   [periodField setDoubleValue:[object period]];
   [priorityField setIntValue:[object priority]];
   [visibleDebugSwitch setState:[object visibleDebug]];

   if ([object wrap])  // check for this first
   {  [behaviorMatrix selectCellWithTag:0];
      [behaviorValueField setEnabled:YES];
      [behaviorText setStringValue:"wrap when value is:"];
   }
   else
   {  if ([object finiteLife])
      { if ([object conditionalLifespan])  // check for this first
        {  [behaviorMatrix selectCellWithTag:3];
           [behaviorValueField setEnabled:NO];
           [behaviorText setStringValue:"see tcl code..."];
        }
        else
        {  [behaviorMatrix selectCellWithTag:1];
           [behaviorValueField setEnabled:YES];
           [behaviorText setStringValue:"die when value is:"];
        }
      }
      else
      {  [behaviorMatrix selectCellWithTag:2];
         [behaviorValueField setEnabled:NO];
         [behaviorText setStringValue:""];
      }
   }

   [syncSwitch setState:[object sync]];
   [syncValueField setIntValue:[object syncValue]];
   if ([object sync])
   {  [syncValueField setEnabled:YES];
   }
   else
   {  [syncValueField setEnabled:NO];
   }
 
   [startValueField setFloatValue:[object startValue]];
   [behaviorValueField setFloatValue:[object wrapValue]];
   [incrementByField setFloatValue:[object incrementBy]];
   [preControlStringText setText:[object preControlString]];
   [conditionalText setText:[object conditional]];
   [theControlStringText setText:[object controlString]];
   [postControlStringText setText:[object postControlString]];

   return self;
}

- revert:sender
{
   [window disableFlushWindow];
   [self revertInspector:sender];
   [window reenableFlushWindow];
   [window flushWindow];
   return [super revert:sender];
}

- setBehavior:sender
{
   if ([[sender selectedCell] tag] == 0) // it's a wrap
   {  [object setWrap:YES];
      [object setFiniteLife:NO];
   }
   else
   {  if ([[behaviorMatrix selectedCell] tag] == 1) // it's days are numbered
      {  [object setWrap:NO];
         [object setFiniteLife:YES];
      }
      else
      {  if ([[behaviorMatrix selectedCell] tag] == 2) // it's days are numbered
         {  [object setWrap:NO];
            [object setFiniteLife:NO];
            [object setConditionalLifespan:NO];
         }
         else  // it's conditional
         {  [object setWrap:NO];
            [object setFiniteLife:YES];
            [object setConditionalLifespan:YES];
         }
      }
   } 
   return [self ok:self];
}

- thresholdPopped:sender
{
   int val;


   val = [[sender selectedCell] tag];
   if (val < 0)
   {  return self;
   }
   [priorityField setIntValue:thresholds[val]];
   return [self ok:self];
}

//
- (BOOL)wantsButtons	{ return YES; }

// text delegate methods
- textDidChange:sender {  return [self touch:sender]; }


@end
